/**
 * 详情对话框
 */
var ClientInfoDlg = {
    data: {
        name: "",
        phone: "",
        address: "",
        createTime: "",
        updateTime: ""
    }
};

layui.use(['form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;

    var selectChargeName;
    var selectBusinessName;


    //让当前iframe弹层高度适应
    admin.iframeAuto();

    loadResultData();

    //加载信息
    function loadResultData() {

        // 人员
        loadChargeNameList();
        //业务人员
        loadBusinessNameList();
    }

    //获取详情信息，填充表单
    if (!Feng.isUndefined(Feng.getUrlParam("clientId"))) {
        var ajax = new $ax(Feng.ctxPath + "/api/client/detail?clientId=" + Feng.getUrlParam("clientId"));
        var result = ajax.start();
        form.val('clientForm', result.data);
        resultData = result.data;
        selectChargeName = resultData.chargeName;
        selectBusinessName = resultData.businessName;

    }

    //=========== 加载工作人员 ========== //

    function loadChargeNameList() {

        $("#chargeNameSelect").html('');
        var ajax = new $ax(Feng.ctxPath + "/mgr/allList", function (data) {
            for (var i = 0; i < data.data.length; i++) {
                var name = data.data[i].name;
                var account = data.data[i].account;
                var value = account + "-" + name;

                var select = '';
                if (value === selectChargeName) {
                    select = 'selected';
                }
                if (i === 0 && Feng.isUndefined(selectChargeName) && value !== selectChargeName) {
                    selectChargeName = value;
                    select = 'selected';
                }
                var selected = select;
                $("#chargeNameSelect").append('<option value="' + value + '"   ' + selected + '>' + value + '</option>');
            }
            form.render();

        }, function (data) {
        });
        ajax.start();
    }

    form.on('select(chargeNameSelect)', function (data) {
        selectChargeName = data.value;
    });
    //=========== 加载业务人员 ========== //

    function loadBusinessNameList() {

        $("#businessNameSelect").html('');
        var ajax = new $ax(Feng.ctxPath + "/mgr/allList", function (data) {
            for (var i = 0; i < data.data.length; i++) {
                var name = data.data[i].name;
                var account = data.data[i].account;
                var value = account + "-" + name;

                var select = '';
                if (value === selectBusinessName) {
                    select = 'selected';
                }
                if (i === 0 && Feng.isUndefined(selectBusinessName) && value !== selectBusinessName) {
                    selectBusinessName = value;
                    select = 'selected';
                }
                var selected = select;
                $("#businessNameSelect").append('<option value="' + value + '"   ' + selected + '>' + value + '</option>');
            }
            form.render();

        }, function (data) {
        });
        ajax.start();
    }

    form.on('select(businessNameSelect)', function (data) {
        selectBusinessName = data.value;
    });

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {


        if (Feng.isUndefined(selectChargeName)) {
            Feng.error("销售人员都不能为空");
            return false;
        }
        var url = "/api/client/addItem";
        var add = true;
        if ($.trim(data.field.clientId) !== '') {
            url = "/api/client/editItem";
            add = false;
        }
        var ajax = new $ax(Feng.ctxPath + url, function (data) {
            if (add === true) {
                Feng.success("新增成功！");
                window.location.href = Feng.ctxPath + '/api/client'
            } else {
                Feng.success("更新成功！");
                window.location.href = Feng.ctxPath + '/api/client'
            }

        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/api/client'
    });

});